package com.bitejiuyeke.forum.controller;

import com.bitejiuyeke.forum.common.AppResult;
import com.bitejiuyeke.forum.common.ResultCode;
import com.bitejiuyeke.forum.config.AppConfig;
import com.bitejiuyeke.forum.model.Message;
import com.bitejiuyeke.forum.model.User;
import com.bitejiuyeke.forum.services.IMessageService;
import com.bitejiuyeke.forum.services.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 666
 * Date: 2024-02-18
 * Time: 15:28
 */
@Slf4j
@Api(tags = "站内信")
@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    private IMessageService messageService;
    @Resource
    private IUserService userService;

    @ApiOperation("发送站内信")
    @PostMapping("/send")
    public AppResult send(HttpServletRequest request,
                          @ApiParam("接受者id") @RequestParam("receiveUserId") @NonNull Long receiverUserId,
                          @ApiParam("接受内容") @RequestParam("content") @NonNull String content) {
        //校验用户当前状态
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        //不能给自己发信

        if (user.getId().equals(receiverUserId)) {
            return AppResult.failed("不能给自己发信");
        }
        //校验接受者是否存在
        User receiveUser = userService.selectById(receiverUserId);
        if (receiveUser == null || receiveUser.getState() == 1) {
            return AppResult.failed("接受者状态异常");
        }
        //封装对象
        Message message = new Message();
        message.setPostUserId(user.getId()); //发送者
        message.setReceiveUserId(receiverUserId); //接受者
        message.setContent(content);
        //调用service
        messageService.create(message);
        return AppResult.success("发送成功");
    }


    @ApiOperation("获取未读消息数量")
    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //调用service
        Integer count = messageService.selectUnreadCount(user.getId());
        //当前登录用户的id，就是接受者的id
        return AppResult.success(count);
    }

    @GetMapping("/getAll")
    @ApiOperation("查询用户的所以站内信")
    public AppResult<List<Message>> getAll(HttpServletRequest request) {
        //获取当前登录的用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //调用service
        List<Message> messages = messageService.selectByReceiveUserId(user.getId());

        //返回结果

        return AppResult.success(messages);
    }

    @ApiOperation("标记已读")
    @PostMapping("/markRead")
    public AppResult markRead(HttpServletRequest request, @ApiParam("站内信id") @RequestParam("id") @NonNull Long id) {

        //根据id查询站内信
        Message message = messageService.selectById(id);
        // 站内信是否存在


        if (message == null || message.getDeleteState() == 1) {
            return AppResult.failed("该站内信不存在");
        }

        //站内信是不是自己的
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getId() != message.getReceiveUserId()) {
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }

        //修改状态
    messageService.updateStateById(id,(byte)1);

        return AppResult.success();
    }

    /**
     * 回复站内信
     * @param request
     * @param repliedId
     * @param content
     * @return
     */
    @ApiOperation("回复站内信")
@PostMapping("/reply")
    public  AppResult reply (HttpServletRequest request,
                          @ApiParam("要回复的站内信id") @RequestParam("repliedId")   @NonNull Long repliedId,
                             @ApiParam("要回复的站内信内容") @RequestParam("content")   @NonNull   String content){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
    if(user.getState() == 1){
        //返回错误描述
        return AppResult.failed(ResultCode.FAILED_USER_BANNED);

    }
        //检查要回复的站内信状态
        Message existsMessage = messageService.selectById(repliedId);
        if (existsMessage == null || existsMessage.getDeleteState() == 1) {
            //返回错误描述
            return AppResult.failed("该站内信不存在");
        }
        //不能给自己回复
        if (existsMessage.getPostUserId() == user.getId()) {

            return AppResult.failed("不能回复自己的站内信");
        }
    //构造对象
        Message message = new Message();
        message.setPostUserId(user.getId());
message.setReceiveUserId(existsMessage.getPostUserId());
   message.setContent(content);
   //调用service
        messageService.reply(repliedId,message);
        return  AppResult.success();
    }
}
